﻿using System.Data;
using System.Data.SqlClient;

namespace Adams.Web.Data.Announcements
{
    public static class Announcement
    {
        public static Models.Announcements.Announcement.PageLoad PageLoad(int id)
        {
            var data = new Models.Announcements.Announcement.PageLoad();

            using (SqlDataReader dr = DB.RunDataReader(@"
SELECT A.* 
FROM Announcements.Announcements A 
WHERE A.AnnouncementID = @AnnouncementID
                ", 
                CommandType.Text,
                DB.CreateParameter("@AnnouncementID", id)))
            {
                if (dr.HasRows)
                {
                    dr.Read();
                    data.Announcement = DB.InitFromReader<Shared.Entities.Announcements.Announcement>(dr, new ColMap { NameInDb = "AnnouncementID", NameInObject = "ID" });
                }

                return data;
            }
        }

        public static Models.Announcements.Announcement.PageLoad PageLoad(string title)
        {
            var data = new Models.Announcements.Announcement.PageLoad();

            using (SqlDataReader dr = DB.RunDataReader(@"
SELECT TOP 1 A.* 
FROM Announcements.Announcements A 
WHERE A.Title = @Title
ORDER BY A.StartDate DESC
                ",
                CommandType.Text,
                DB.CreateParameter("@Title", title)))
            {
                if (dr.HasRows)
                {
                    dr.Read();
                    data.Announcement = DB.InitFromReader<Shared.Entities.Announcements.Announcement>(dr, new ColMap { NameInDb = "AnnouncementID", NameInObject = "ID" });
                }

                return data;
            }
        }
    }
}
